package com.broadtext.message.service.impl;

import com.broadtext.common.bean.EntityPageBean;
import com.broadtext.common.exp.DaoException;
import com.broadtext.common.exp.ServiceException;
import com.broadtext.common.repository.EntityDao;
import com.broadtext.common.service.impl.EntityServiceImpl;
import com.broadtext.common.utils.UUIDUtils;
import com.broadtext.message.model.MsgNoticeConfig;
import com.broadtext.message.model.NoticeTime;
import com.broadtext.message.service.IMsgNoticeConfigService;
import com.broadtext.rms.system.model.TtAlertDefinition;
import com.github.miemiedev.mybatis.paginator.domain.PageBounds;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service("msgNoticeConfigService")
public class MsgNoticeConfigServiceImpl extends EntityServiceImpl<MsgNoticeConfig> implements IMsgNoticeConfigService {

    /**
     *
     * <p>Description: 基类dao</p>
     * @param entityDao 基类实体dao
     */
    @Autowired
    @Qualifier("msgNoticeConfigDao")
    public void setEntityDao(EntityDao<MsgNoticeConfig> entityDao) {
        super.entityDao = entityDao;
    }

    @Override
    @SuppressWarnings("unchecked")
    public EntityPageBean<MsgNoticeConfig> getMsgNoticeConfigPageBean(Map<String, Object> params, PageBounds pageBounds) throws ServiceException, DaoException {
        EntityPageBean<MsgNoticeConfig> pageBean = super.findEntityPageByQuery(params, pageBounds);
        List<MsgNoticeConfig> list = pageBean.getResults();
        Map<String, Object> queryNoticeParams = new HashMap<>();
        for (MsgNoticeConfig msgNoticeConfig : list) {
            queryNoticeParams.put("receiver",msgNoticeConfig.getReceiver());
            List<NoticeTime> noticeTimes = this.entityDao.findByProperty("com.broadtext.message.model.MsgNoticeConfig.getNoticeTime",queryNoticeParams);
            msgNoticeConfig.setNoticeTimes(noticeTimes);
            List<TtAlertDefinition> alertDefinitions = this.entityDao.findByProperty("com.broadtext.message.model.MsgNoticeConfig.getNoticeAlerts",queryNoticeParams);
            msgNoticeConfig.setAlertDefinitions(alertDefinitions);
        }
        pageBean.setResults(list);
        return pageBean;
    }

    @Override
    @SuppressWarnings("unchecked")
    public MsgNoticeConfig getMsgNoticeConfig(String id) throws DaoException {
        if (StringUtils.isBlank(id))
            return null;
        List<MsgNoticeConfig> list = this.entityDao.findByProperty("com.broadtext.message.model.MsgNoticeConfig.selectMsgNoticeConfig",id);
        if (list!=null&&list.size()>0&&list.get(0)!=null) {
            MsgNoticeConfig msgNoticeConfig = list.get(0);
            Map<String, Object> queryNoticeParams = new HashMap<>();
            queryNoticeParams.put("receiver",msgNoticeConfig.getReceiver());
            List<NoticeTime> noticeTimes = this.entityDao.findByProperty("com.broadtext.message.model.MsgNoticeConfig.getNoticeTime",queryNoticeParams);
            msgNoticeConfig.setNoticeTimes(noticeTimes);
            List<TtAlertDefinition> alertDefinitions = this.entityDao.findByProperty("com.broadtext.message.model.MsgNoticeConfig.getNoticeAlerts",queryNoticeParams);
            msgNoticeConfig.setAlertDefinitions(alertDefinitions);
            return msgNoticeConfig;
        }
        return null;
    }

    @Override
    public void saveMsgNoticeConfig(MsgNoticeConfig msgNoticeConfig,String operator) throws DaoException, ServiceException {
        if (msgNoticeConfig == null|| StringUtils.isBlank(msgNoticeConfig.getReceiver())) {
            throw new ServiceException("参数错误");
        }
        //更新用户的通知标志位
        this.entityDao.updateRecord("com.broadtext.message.model.MsgNoticeConfig.updateMsgNoticeFlag",msgNoticeConfig);
        //删除消息通知告警类型配置表中的数据
        this.entityDao.deleteRecord("com.broadtext.message.model.MsgNoticeConfig.deleteMsgNoticeAlertConfig",msgNoticeConfig.getReceiver());
        //插入消息通知告警类型数据
        for (TtAlertDefinition alert:msgNoticeConfig.getAlertDefinitions()) {
            MsgNoticeConfig t = new MsgNoticeConfig();
            t.setPkId(UUIDUtils.getUUID());
            t.setAlertCode(alert.getAlertCode());
            t.setReceiver(msgNoticeConfig.getReceiver());
            t.setDelFlag("0");
            t.setCreateUser(operator);
            t.setCreateDate(new Date());
            this.entityDao.insertRecord("com.broadtext.message.model.MsgNoticeConfig.insertMsgNoticeAlertConfig",t);
        }
        //删除消息通知时间配置表中的数据
        this.entityDao.deleteRecord("com.broadtext.message.model.MsgNoticeConfig.deleteMsgNoticeTimeConfig",msgNoticeConfig.getReceiver());
        //插入新数据
        for (NoticeTime noticeTime:msgNoticeConfig.getNoticeTimes()) {
            MsgNoticeConfig t = new MsgNoticeConfig();
            t.setPkId(UUIDUtils.getUUID());
            t.setStartTime(noticeTime.getStartTime());
            t.setEndTime(noticeTime.getEndTime());
            t.setWeek(noticeTime.getWeek());
            t.setReceiver(msgNoticeConfig.getReceiver());
            t.setDelFlag("0");
            t.setCreateUser(operator);
            t.setCreateDate(new Date());
            this.entityDao.insertRecord("com.broadtext.message.model.MsgNoticeConfig.insertMsgNoticeTimeConfig",t);
        }
    }
}
